﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Data.SQLite;
using System.Data;
using Pub;
using JT_lzqx;

namespace LZQXT.Test.Forms.DB
{
    public partial class FDBInit : Form
    {

        public static string S_Con;

        //public string DBCon;
        public string access_con;

        //public DBCon dbCon = new DBCon();
        //public CDBTree_Access dbCon = new CDBTree_Access();
        public CDBTree dbCon = new CDBTree();

        public FDBInit()
        {
            InitializeComponent();
        }

        private void FDBInit_Load(object sender, EventArgs e)
        {
            lstBox_ConName.Dock = DockStyle.Fill;
            panel2.Dock = DockStyle.Fill;
            tbox_ConStr.Dock = DockStyle.Fill;

            db_init();
        }

        public void db_init()
        {
            lstBox_Server.Items.Clear();
            for (int i = 0; i < dbCon.lstDBInf.Count; i++)
            {
                lstBox_Server.Items.Add(dbCon.lstDBInf[i].sServ);
            }

            if (lstBox_Server.Items.Count > 0) lstBox_Server.SelectedIndex = 0;
        }

        private void FDBInit_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        /// <summary>
        /// 选择连接类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstBox_Server_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                fillDBInf();

                lstBox_ConName.Items.Clear();
                List<string> lstDBNamS = new List<string>();
                lstDBNamS = dbCon.getDBNam(lstBox_Server.Text);
                for (int i = 0; i < lstDBNamS.Count; i++)
                {
                    lstBox_ConName.Items.Add(lstDBNamS[i]);
                }

                if (lstBox_ConName.Items.Count > 0) lstBox_ConName.SelectedIndex = 0;

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }

        private void lstBox_ConName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sServ = lstBox_Server.Text;
            string sD_B_ = lstBox_ConName.Text;
            string strDBCon = dbCon.getDBCon(sServ, sD_B_);
            tbox_ConStr.Text = strDBCon;

            fillDBInf();
        }

        public void fillDBInf()
        {
            string sServ = lstBox_Server.Text;
            FDBTree_lzqx.fdbinit_add.s_ID = dbCon.getDBInfo(sServ)[0].sID;
            FDBTree_lzqx.fdbinit_add.tboxDB.Text = dbCon.getDBInfo(sServ)[0].sD_B_;
            FDBTree_lzqx.fdbinit_add.tboxServer.Text = dbCon.getDBInfo(sServ)[0].sServ;
            FDBTree_lzqx.fdbinit_add.tboxUser.Text = dbCon.getDBInfo(sServ)[0].sUser;
            FDBTree_lzqx.fdbinit_add.tboxPass.Text = dbCon.getDBInfo(sServ)[0].sPass;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FDBTree_lzqx.fdbinit_add.i_Typ = 1;
            FDBTree_lzqx.fdbinit_add.ShowDialog();

            db_init();
        }

        private void btnEdt_Click(object sender, EventArgs e)
        {
            FDBTree_lzqx.fdbinit_add.i_Typ = 2;
            FDBTree_lzqx.fdbinit_add.ShowDialog();

            db_init();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string strSQL = "DELETE FROM T_DBCon WHERE ID = " + FDBTree_lzqx.fdbinit_add.s_ID;
            if (CPub.MsgBox(strSQL, "删除数据"))
            {
                dbCon.cmd1(strSQL);
            }

            db_init();
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            db_init();
        }

    }
}
